----------------------------------
-- 对象选择
----------------------------------
local CombatImpl = require("combat.CombatImpl")
local CombatObj = require("combat.CombatObj")
local CombatDefine = require("combat.CombatDefine")
local RoleDefine = require("role.RoleDefine")
local CombatCalc = require("combat.CombatCalc")
local CombatBuff = require("combat.CombatBuff")
local SkillExcel = require("excel.skill").skill
local targets = {}

--对象选择器begin

local function getTargetSide(attacker,targetMode)
	if targetMode[2] == 0 then
		return 0
	elseif targetMode[2] == 1 then
		if attacker.side == CombatDefine.ATTACK_SIDE then
			return CombatDefine.DEFEND_SIDE
		else
			return CombatDefine.ATTACK_SIDE
		end
	elseif targetMode[2] == 2 then
		return attacker.side
	end
end

local function canTarget(obj)

	if obj and obj.hp and  obj.hp > 0 and not CombatBuff.isStatus(obj,{"revive"}) and CombatDefine.isBackup(obj) ~= CombatDefine.BACKUP_TYPE1 then
		return true
	end
end

-- 同列敌方找不到 随机上下找一列 有敌方的    策划 要求随机
local function getColumn2PosRandom(yetList)
	local randomList = {}

	for i= 1, 3 do
		if not yetList[i] then
			randomList[#randomList + 1] = i
		end
	end

	if #randomList > 0 then
		local random = math.random(1, #randomList)
		local newColumn = randomList[random]
		yetList[newColumn] = true
		return newColumn, yetList
	end
	return 0, yetList
end

function getObj(pos)
    -- 不能直接取 援军对象
    if pos == CombatDefine.BACKUP_POS[1] or  pos == CombatDefine.BACKUP_POS[2] then return end

	local obj = CombatImpl.objList[pos]
	local backupPos = pos < CombatDefine.COMBAT_HERO_CNT and CombatDefine.BACKUP_POS[1] or CombatDefine.BACKUP_POS[2]
	local backupObj = CombatImpl.objList[backupPos]
    -- 援军已上场
	if backupObj and backupObj.backupPos == pos then
		return backupObj
	end
	return obj
end

--
CAN_EMPTY_TARGET = { [22] = 1 }
function getTargets(attacker, targetMode, skillTargets, cmdTargets, checkChaofen, isNormalAtk, skillID)
    for i = 1, #targets do
        targets[i] = nil
    end
    
    handlerInit(attacker, targetMode)

    if isNormalAtk then
        local isStatus, cmd, ind = CombatBuff.isStatus(attacker, { "tiaoxin" })
        if isStatus then
            local buffer = attacker.buffer[ind]
            local target = buffer.attackPos and getObj(buffer.attackPos)
            if canTarget(target) then
                targets[#targets + 1] = target
            end
        elseif attacker.normalTarget and attacker.normalTarget.status then
            handlerStatus(attacker, attacker.normalTarget.status)
        end
    end
    if targets[1] == nil then
        if checkChaofen then
            -- 检查是否被嘲讽
            local chaofen, fromPos = CombatBuff.isChaofen(attacker)
            if chaofen then
                local obj = getObj(fromPos)
                if canTarget(obj) and obj.pos ~= attacker.pos then
                    return { obj }, true
                end
            end
        end
        
        if skillID and CombatBuff.isStatus(attacker, { "hunluan" }) and CombatImpl.isFanji == false then
            handlerHunluan(attacker, targetMode)
        elseif handler[targetMode[1]] then
            handler[targetMode[1]](attacker, targetMode, skillTargets, cmdTargets, skillID)
        end

        if not CAN_EMPTY_TARGET[targetMode[1]] and targetMode[2] == 1 and targets[1] == nil and targetMode[1] ~= 32 then
            handler[1](attacker, targetMode, skillTargets)
        end

        handlerFenTan(attacker, targetMode, skillTargets, cmdTargets, skillID)
    end
    local ret = { }
    for _, v in ipairs(targets) do
        ret[#ret + 1] = v
    end
    return ret
end

--默认顺序位置
function handler1(attacker,targetMode)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end

--全体
function handler2(attacker,targetMode)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end

--前排，中排，后排
function handler3(attacker,targetMode)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end

--最近一列
function handler4(attacker,targetMode)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end

--随机前，中，后排
function handler5(attacker,targetMode)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end

--随机
function handler6(attacker,targetMode)
	local targetSide = getTargetSide(attacker,targetMode)
	if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
		assert()
	end
	local cnt = targetMode[3] or 1
	local noSelf = targetMode[5]
	local temp = {}
	for i = 1,CombatDefine.COMBAT_HERO_CNT do
		local pos = CombatDefine.SIDE2POS[targetSide][i]
		if not (noSelf and pos == attacker.pos) then
			local obj = getObj(pos)
			if canTarget(obj) then 
				temp[#temp+1] = obj
			end
		end
	end
	

	if #temp > 0 then
		for i = 1,cnt do
			local r = math.ceil(math.random(0,#temp))
			r = r == 0 and 1 or r
			targets[#targets+1] = temp[r]
			temp[r] = temp[#temp]
			temp[#temp] = nil
			if #temp <= 0 then
				break
			end
		end
	end
	if noSelf == 2 and canTarget(attacker) then
		targets[#targets+1] = attacker
	end
end

--血量最少
function handler7(attacker,targetMode)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end

--血量最高
function handler8(attacker,targetMode)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end

--攻击最高
function handler9(attacker,targetMode)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end

end

--自己
function handler10(attacker,targetMode,skillTargets)
	--if canTarget(obj) then
		targets[1] = attacker
	--end
end

--技能目标（所有目标）
function handler11(attacker,targetMode,skillTargets)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end


--技能目标（指定第几个目标）
function handler12(attacker,targetMode,skillTargets)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end

--被击时攻击者
function handler13(attacker,targetMode,skillTargets)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end

--额外作用目标
function  handler14(attacker,targetMode,skillTargets,cmdTargets)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end

--技能目标(生命高于或者低于攻击方)
function handler15(attacker,targetMode,skillTargets)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end

--技能目标(护甲高于或者低于攻击方)
function handler16(attacker,targetMode,skillTargets)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end


--血量百分比最少
function handler17(attacker,targetMode)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end


--血量最少,优先选择非满血的英雄
function handler18(attacker,targetMode)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end

--随机后排,优先选择非满血的英雄
function handler19(attacker,targetMode)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end

--随机,优先选择非满血的英雄
function handler20(attacker,targetMode)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end

--全体
function handler21(attacker,targetMode)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end

--非技能目标
function handler22(attacker,targetMode,skillTargets)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end

--技能目标(速度高于或者低于攻击方)
function handler23(attacker,targetMode,skillTargets)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end


--职业+攻击力排名
function handler24(attacker,targetMode)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end

--职业优先
function handler25(attacker,targetMode)
	local targetSide = getTargetSide(attacker,targetMode)
	if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
		assert()
	end
	local cnt = targetMode[3] or 1
	local job = targetMode[4]
	local temp = {}
	for _,pos in ipairs(CombatDefine.SIDE2POS[targetSide]) do
		local obj = getObj(pos)
		if canTarget(obj) and obj.job == job then
			targets[#targets + 1] = obj
		end

		if #targets >= cnt then
			break
		end
	end
	if #targets == 0 then
		handler1(attacker,targetMode)
	end
	return
end


--同列
function handler26(attacker,targetMode)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end

--默认顺序位置2
function handler27(attacker,targetMode)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end


--前排，中排，后排
function handler28(attacker,targetMode)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end


--随机 选择目标 多次
function handler29(attacker,targetMode)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end


--攻击最高
function handler30(attacker,targetMode)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end

--前排，中排，后排
function handler31(attacker,targetMode)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end


-- 根据指定buff层数选择目标
function handler32(attacker, targetMode, skillTargets, cmdTargets, skillID)
    local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end

--随机攻击防御速度最高
function handler33(attacker,targetMode)
	local targetSide = getTargetSide(attacker,targetMode)
	--if targetSide ~= CombatDefine.ATTACK_SIDE and targetSide ~= CombatDefine.DEFEND_SIDE then
	--	assert()
	--end
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
	local atkPos = attacker.backupPos or attacker.pos
	atkPos = atkPos % CombatDefine.COMBAT_HERO_ALL_CNT
	for _,pos in ipairs(CombatDefine.DEFAULT_TARGET_POS[atkPos]) do
		local obj = getObj(pos)

		if canTarget(obj)  and obj.pos ~= attacker.pos then 
			targets[#targets+1] = obj
		end
		if #targets >= cnt then
			return 
		end
	end
end


function handlerInit(attacker, targetMode)
    -- 清除分摊标志
    attacker.handBuffCalcType = nil
    for _, pos in ipairs(CombatDefine.SIDE2POS[0]) do
       local obj = getObj(pos)
       if obj then
           obj.fentq = nil
       end
    end
end

-- 处理所有对象是否需要分摊
function handlerFenTan(attacker, targetMode, skillTargets, cmdTargets, skillID)
    if #targets <= 0 then return end

    if skillID then
       local skillConfig = SkillExcel[skillID]
       if skillConfig then 
          if skillConfig.otherArgs and skillConfig.otherArgs.hurtFenTan then
             return 
          end
       end 
    end

    local tagObj = targets[1]
    local targetSide = tagObj.side
    local fentq = {}
    local fentCnt = 0
    for _, pos in ipairs(CombatDefine.SIDE2POS[targetSide]) do
       local obj = getObj(pos)
       if obj and canTarget(obj) and obj.bufferCmd["fentan"] then
           fentq[pos] = 1
           fentCnt = fentCnt + 1
       end
    end

    -- 分摊比列
    for _, pos in ipairs(CombatDefine.SIDE2POS[targetSide]) do
       local obj = getObj(pos)
       if obj and canTarget(obj) and obj.bufferCmd["fentan"] then
           obj.fentq = fentCnt

           -- 把不在队列的分摊对象加入到队列当中
           local find = false
           for _, tarobj in ipairs(targets) do
              if tarobj.pos == obj.pos then
                 find = true
              end
           end

           if not find then
              targets[#targets + 1] = obj
           end
       end
    end
              
end

function handlerHunluan(attacker,targetMode)
	local cnt = targetMode[3] or CombatDefine.COMBAT_HERO_ALL_CNT
    local len = 0
    local selectList = {}
	for pos = 1,CombatDefine.COMBAT_HERO_ALL_CNT do
		local obj = getObj(pos)
		if canTarget(obj) then 
            len = len + 1
            selectList[len] = obj
		end
	end
   
    -- 混乱要随机选择目标 modify buy dxzeng
    local randomLen = #selectList
    if randomLen <= 0 or cnt <= 0 then return end
    
    if cnt > randomLen then 
       cnt = randomLen
    end
    
    for index = 1, cnt do
       local len = 0
       local tempRandom = {} 
       for k, v in pairs(selectList) do
          len = len + 1
          tempRandom[len] = k
       end
       
       local randomIndex = math.random(1, len)
       local selectIndex = tempRandom[randomIndex]
       targets[#targets+1] = selectList[selectIndex]
       selectList[selectIndex] = nil
    end
end

function handlerStatus(attacker,status)
	local targetSide
	if attacker.side == CombatDefine.ATTACK_SIDE then
		targetSide = CombatDefine.DEFEND_SIDE
	else
		targetSide = CombatDefine.ATTACK_SIDE
	end
	for _,pos in ipairs(CombatDefine.SIDE2POS[targetSide]) do
		local obj = getObj(pos)
		if canTarget(obj) and CombatBuff.isStatus(obj,status) then 
			targets[#targets+1] = obj
			return
		end
	end
end

handler = handler or {}	--选择策略
handler[1] = handler1	--1 默认顺序位置
handler[2] = handler2	--2 全体
handler[3] = handler3	--3 前排，中排，后排
handler[4] = handler4	--4 前方一列
handler[5] = handler5	--5 随机前排，中排，后排
handler[6] = handler6	--6 随机
handler[7] = handler7	--7 血量最少
handler[8] = handler8	--8 血量最高
handler[9] = handler9	--9 攻击最高
handler[10] = handler10	--10 自己
handler[11] = handler11	--11 技能目标（所有目标）
handler[12] = handler12	--12 技能目标（指定第几个目标）
handler[13] = handler13	--13 被击时攻击者
handler[14] = handler14	--14 额外作用目标
handler[15] = handler15	--15 技能目标(生命高于或者低于攻击方)
handler[16] = handler16	--16 技能目标(护甲高于或者低于攻击方)
handler[17] = handler17	--17 技能目标(血量百分比最少)
handler[18] = handler18	--18 技能目标(血量最少,优先选择非满血的英雄)
handler[19] = handler19	--19 随机后排,优先选择非满血的英雄
handler[20] = handler20	--20 随机,优先选择非满血的英雄
handler[21] = handler21	--21 死亡武将
handler[22] = handler22	--22 非技能目标
handler[23] = handler23	--23 技能目标(速度高于或者低于攻击方)
handler[24] = handler24	--24 职业+攻击力排名
handler[25] = handler25	--25 职业优先
handler[26] = handler26	--26 同一列
handler[27] = handler27	--27 同一列
handler[28] = handler28	--27 同一列
handler[29] = handler29	--27 同一列
handler[30] = handler30	--30 自己跟目标攻击最高
handler[31] = handler31	--31 同行人数最多
handler[32] = handler32	--32 指定buff选择
handler[33] = handler33	--32 指定buff选择

--对象选择器end